package com.longrise.android.byjk.plugins.aboutme.personalInfo.person_honor;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.PrintLog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/3/8.
 * 个人荣誉的presenter
 */

public class PersonHonorShowPresenter extends PersonHonorShowContract.Presenter{

    public static final String TAG = "PersonHonorShowPresenter";
    @Override
    protected void init() {

    }

    //查询个人荣誉
    @Override
    public void getPersonHonorData() {

        String usersfzh = UserInfor.getInstance().getUsersfzh();
        String userid = UserInfor.getInstance().getUserid();
        EntityBean bean = new EntityBean();
        bean.set("cardno", usersfzh);
        bean.set("userid ", userid);
        EntityBean bean1 = new EntityBean();
        bean1.set("bean", bean);
        mView.showNormalLoadingPage();
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "insh_user_spersonalhonor", bean1, new LoadDataManager.OnRequestCallListener2() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean bean = (EntityBean) result;
                    int resultstate = bean.getInt("resultstate");
                    if(resultstate == 1){
                        parseHonorData(bean);
                    }else {
                        mView.showToast(bean.getString("resultdesc"));
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            @Override
            public void onFinished(String key, String service) {
               mView.showContentPage();
            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

            }

        });
    }


    public void parseHonorData(EntityBean bean){
        EntityBean[] results = bean.getBeans("result");
        List<PersonHonorBean> mHonorBeanList = new ArrayList<>();
        if (results != null && results.length != 0) {
            for (EntityBean resultBean : results) {
                PersonHonorBean honorBean = new PersonHonorBean();
                honorBean.setHonordesc(resultBean.getString("personalhonordesc"));
                honorBean.setHonorId(resultBean.getString("id"));
                mHonorBeanList.add(honorBean);
            }
        }
        PrintLog.e(TAG,"mHonorBeanList=:"+ mHonorBeanList);
        mView.refreshData(mHonorBeanList);
    }


    //删除个人荣誉
    @Override
    public void deletePersonHonor(List<String> honorIds) {
        EntityBean honorBean = new EntityBean();
        EntityBean bean = new EntityBean();
        bean.set("id",honorIds);
        bean.set("userid",UserInfor.getInstance().getUserid());
        bean.set("cardno",UserInfor.getInstance().getUsersfzh());
        bean.set("delecttype","1");
        honorBean.set("bean", bean);
        mView.showLoadingDialog();
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "insh_user_deletemethod", honorBean, new LoadDataManager.OnRequestCallListener2() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean bean = (EntityBean) result;
                    int resultstate = bean.getInt("resultstate");
                    if(resultstate == 1){
                        mView.showToast(bean.getString("resultdesc"));
                        mView.responseDelteData(bean);
                    }else {
                        mView.showToast(bean.getString("resultdesc"));
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            @Override
            public void onFinished(String key, String service) {
                mView.dismissLoadingDialog();
            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

            }

        });
    }
}
